/**
 * 
 */

package cn.org.seventh.web.tag;

import java.io.IOException;
import java.net.InetAddress;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

/**
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since 2007-11-28 下午09:37:12
 * @jsp.tag name="basepath" body-content="empty" description="set base path" please define a context param value name "basepath" and the value is the related path
 */
public class BasePathTag implements Tag {

    private static String BASE;

    private static String LOCAL;

    private static String LOCAL_IP;

    private static String MASK_IP;

    private static long localip;

    private static long maskip;

    private String path;

    private PageContext pageContext;

    /**
     * @jsp.attribute type="string" required="false" rtexprvalue="flase" description="set the related path url"
     * @param path
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
     */
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
     */
    public int doStartTag() throws JspException {
        String url = null;
        try {
            String ip = pageContext.getRequest().getRemoteAddr();
            System.out
                .println("==============================================================");
            System.out.println(ip);
            long remoteip = ip2Number(ip);
            if ((remoteip & maskip) == (localip & maskip)) {
                url = LOCAL;
                System.out
                    .println("local net!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            } else {
                url = BASE;
            }
            System.out
                .println("==============================================================");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            if (null == url) {
                url = BASE;
            }
            // TODO for test
            pageContext.getOut().write("<base href=\"" + url + "\"/>");
            // pageContext.getOut().write("<base href=\"" + "/" + "\"/>");
            // pageContext.getOut().write("");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#getParent()
     */
    public Tag getParent() {
        return null;
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#release()
     */
    public void release() {
        path = null;
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#setPageContext(javax.servlet.jsp.PageContext)
     */
    public void setPageContext(PageContext context) {
        pageContext = context;
        BASE = pageContext.getServletContext().getInitParameter("basepath");
        LOCAL = pageContext.getServletContext().getInitParameter("localpath");
        LOCAL_IP = pageContext.getServletContext().getInitParameter("localip");
        MASK_IP = pageContext.getServletContext().getInitParameter("maskip");
        if (null == BASE) {
            BASE =
                "http://" + pageContext.getRequest().getServerName() + ":"
                    + pageContext.getRequest().getServerPort() + "/"
                    + pageContext.getServletContext().getContext("/");
        }
        if (null == LOCAL) {
            LOCAL =
                "http://" + pageContext.getRequest().getServerName() + ":"
                    + pageContext.getRequest().getServerPort() + "/"
                    + pageContext.getServletContext().getContext("/");
        }
        if (null == LOCAL_IP) {
            try {
                LOCAL_IP = InetAddress.getLocalHost().toString();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (null == MASK_IP) {
            MASK_IP = "255.255.255.0";
        }
        try {
            localip = ip2Number(LOCAL_IP);
            maskip = ip2Number(MASK_IP);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#setParent(javax.servlet.jsp.tagext.Tag)
     */
    public void setParent(Tag arg0) {
    }

    public static long ip2Number(String ip) throws Exception {
        InetAddress address = InetAddress.getByName(ip.trim());
        byte[] bs = address.getAddress();
        long a = bs[0] < 0 ? 256 + bs[0] : bs[0];
        a <<= 24;
        long b = bs[1] < 0 ? 256 + bs[1] : bs[1];
        b <<= 16;
        long c = bs[2] < 0 ? 256 + bs[2] : bs[2];
        c <<= 8;
        long d = bs[3] < 0 ? 256 + bs[3] : bs[3];
        d <<= 0;
        Long number = Long.valueOf(a + b + c + d);
        return number.longValue();
    }
}
